

package pos.ui.screen.administrator;

import javax.swing.JOptionPane;
import pos.control.AddItemUPCControl;
import pos.entity.Item;
import pos.interfaces.Messages;
import java.math.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import pos.control.ItemViewControl;
import pos.control.RetriveCategoryControl;
import pos.storage.db.sql.SQL;

/**
 * This class is used for add an item into the repository.
 * @author wei zhang
 */
public class AddItemUPCPanel extends javax.swing.JFrame {

    ItemView theView = new ItemView();
    /** Creates new form AddItemUPCPanel */
    public AddItemUPCPanel() {
        ArrayList list = new ArrayList();
        initComponents();
        RetriveCategoryControl r1 = new RetriveCategoryControl();
        list = r1.retriveCategory();
        categoryCombobox.removeAllItems();
        for(int i = 0; i < list.size(); i++ ){
            categoryCombobox.addItem(list.get(i));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nameTF = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        productCodeTF = new javax.swing.JTextField();
        CloseButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        descriptionTF = new javax.swing.JTextField();
        basePriceTF = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        dateManufactureTF = new javax.swing.JTextField();
        expiryTF = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        addItemButton = new javax.swing.JButton();
        ClearButton = new javax.swing.JButton();
        jErrorLabel = new javax.swing.JLabel();
        categoryCombobox = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(797, 659));
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabel1.setText("Add Item");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(320, 10, 147, 42);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel2.setText("Name:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(80, 90, 70, 40);

        nameTF.setFont(new java.awt.Font("Tahoma", 0, 18));
        getContentPane().add(nameTF);
        nameTF.setBounds(230, 90, 430, 40);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel3.setText("UPC:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(80, 160, 70, 40);

        productCodeTF.setFont(new java.awt.Font("Tahoma", 0, 18));
        getContentPane().add(productCodeTF);
        productCodeTF.setBounds(230, 160, 430, 40);

        CloseButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        CloseButton.setText("Close");
        CloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseButtonActionPerformed(evt);
            }
        });
        getContentPane().add(CloseButton);
        CloseButton.setBounds(540, 570, 100, 40);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel4.setText("Base Price:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(80, 230, 118, 40);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel5.setText("Description:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(80, 300, 125, 40);

        descriptionTF.setFont(new java.awt.Font("Tahoma", 0, 18));
        getContentPane().add(descriptionTF);
        descriptionTF.setBounds(230, 300, 430, 40);

        basePriceTF.setFont(new java.awt.Font("Tahoma", 0, 18));
        basePriceTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                basePriceTFActionPerformed(evt);
            }
        });
        getContentPane().add(basePriceTF);
        basePriceTF.setBounds(230, 230, 430, 40);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel7.setText("Date of ");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(80, 350, 120, 40);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel8.setText("Expiry Date:");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(80, 440, 130, 40);

        dateManufactureTF.setFont(new java.awt.Font("Tahoma", 0, 18));
        getContentPane().add(dateManufactureTF);
        dateManufactureTF.setBounds(230, 370, 430, 40);

        expiryTF.setFont(new java.awt.Font("Tahoma", 0, 18));
        getContentPane().add(expiryTF);
        expiryTF.setBounds(230, 440, 430, 40);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel6.setText("Manufacture:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(80, 380, 141, 40);

        addItemButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        addItemButton.setText("Add");
        addItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemButtonActionPerformed(evt);
            }
        });
        getContentPane().add(addItemButton);
        addItemButton.setBounds(240, 570, 110, 40);

        ClearButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        ClearButton.setText("Clear");
        ClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearButtonActionPerformed(evt);
            }
        });
        getContentPane().add(ClearButton);
        ClearButton.setBounds(380, 570, 110, 40);
        getContentPane().add(jErrorLabel);
        jErrorLabel.setBounds(230, 50, 430, 30);

        categoryCombobox.setFont(new java.awt.Font("Tahoma", 0, 18));
        categoryCombobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        getContentPane().add(categoryCombobox);
        categoryCombobox.setBounds(230, 501, 430, 40);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel9.setText("Category:");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(80, 495, 130, 40);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed
        this.dispose();
}//GEN-LAST:event_CloseButtonActionPerformed

    private void basePriceTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_basePriceTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_basePriceTFActionPerformed

    private void addItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemButtonActionPerformed
        // TODO add your handling code here:
        String categoryId= "";
        boolean isDataValid = true;
        AddItemUPCControl adminControl = new AddItemUPCControl();
        Item item = new Item();
        String name = nameTF.getText().trim();
        if(name.equals("")){
            jErrorLabel.setText("Please enter the Item name\n"+ " " + jErrorLabel.getText());
            isDataValid = false;
        }
        String productCode = productCodeTF.getText().trim();
        if(productCode.equals("")){
            jErrorLabel.setText("Please enter the Item UPC\n"+ " " + jErrorLabel.getText());
            isDataValid = false;
        }
        //BigInteger item_id = new BigInteger(ItemIDTF.getText());
        Double price = 0.0;
        try{
            price = Double.parseDouble(basePriceTF.getText().trim());
        } catch (NumberFormatException e) {
            jErrorLabel.setText("Please enter a valid price.");
            isDataValid = false;
        };
        
        String desc = descriptionTF.getText().trim();
        if(desc.equals("")){
            jErrorLabel.setText("Please enter the Item description\n"+ " " + jErrorLabel.getText());
            isDataValid = false;
        }

        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date dm = null;
        Date exp = null;
        try {
            dm = df.parse(dateManufactureTF.getText());
            exp = df.parse(expiryTF.getText());
        } catch (ParseException e) {
            jErrorLabel.setText("Please enter dates in DD/MM/YYYY format.");
            isDataValid = false;
        };
        
        if(isDataValid){
            item.setName(name);
            item.setUPCode(productCode);
            //item.setID(BigInteger.valueOf(4));
            item.setBasePrice(price);
            item.setDesc(desc);
            item.setDateTimeOfManufacture(new java.sql.Date(dm.getTime()));
            item.setExpiryDateTime(new java.sql.Date(exp.getTime()));

            categoryId = categoryCombobox.getSelectedItem().toString().charAt(categoryCombobox.getSelectedItem().toString().length()-1)+"";
            //System.out.println(categoryCombobox.getSelectedItem());

            boolean status = adminControl.add(item, categoryId);
            if(status){
                JOptionPane.showMessageDialog(this, Messages.SuccessData, Messages.infoDialogTitle, JOptionPane.PLAIN_MESSAGE);
                refreshTable();
                this.dispose();
            } else{
                JOptionPane.showMessageDialog(this, Messages.FailData, Messages.infoDialogTitle, JOptionPane.ERROR_MESSAGE);
            }
        }
}//GEN-LAST:event_addItemButtonActionPerformed

    private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed
        // TODO add your handling code here:
        clearScreen();
}//GEN-LAST:event_ClearButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddItemUPCPanel().setVisible(true);
            }
        });
    }*/
    /**
     * This method cleans the screen.
     */
    public void clearScreen(){
        productCodeTF.setText("");
        basePriceTF.setText("");
        dateManufactureTF.setText("");
        descriptionTF.setText("");
        expiryTF.setText("");
        nameTF.setText("");
    }
   /**
    * Set the item view object.
    * @param aView
    */
    public void setItemView(ItemView aView){
        theView = aView;

    }
    /**
     * This method refresh the table in the item view panel.
     */
    public void refreshTable(){
        ItemViewControl adminControl = new ItemViewControl();
        theView.sTableModel.removeAll();
        adminControl.genTable(theView.sTableModel, SQL.DISPALY_TABLE);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ClearButton;
    private javax.swing.JButton CloseButton;
    private javax.swing.JButton addItemButton;
    private javax.swing.JTextField basePriceTF;
    private javax.swing.JComboBox categoryCombobox;
    private javax.swing.JTextField dateManufactureTF;
    private javax.swing.JTextField descriptionTF;
    private javax.swing.JTextField expiryTF;
    private javax.swing.JLabel jErrorLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField nameTF;
    private javax.swing.JTextField productCodeTF;
    // End of variables declaration//GEN-END:variables

}
